package mvc;

import java.rmi.RemoteException;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.swing.SwingUtilities;

import model.ModelShop;
import netzwerk.TimeServerThread;
import view.ViewCustomer;
import view.ViewShop;
import Chat.ChatServer;
import controller.ControllerCustomer;
import controller.ControllerShop;

public class Main {

	private static ViewCustomer viewCustomer;
	static AtomicBoolean timeThreadActive = new AtomicBoolean(true);

	public static void main(String[] args) {

		// ChatServer erstellen
		Thread chatThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					new ChatServer();
				} catch (RemoteException e) {
					System.err.println("ChatServer konnte nicht erstellt werden.");
					e.printStackTrace();
				}

			}

		});
		chatThread.run();

		// Instanz des Models erzeugen
		final ModelShop modelShop = new ModelShop();

		// Testprodukte erstellen
		try {
			modelShop.add(new model.Product("p1", 2.5, 6));
			modelShop.add(new model.Product("p2", 7, 1000));
			modelShop.add(new model.Product("p3", 3.55, 0));
			modelShop.add(new model.Product("p4", 55, 1));
			modelShop.add(new model.Product("p5", 1, 2));
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Instanzen der Views, Controller erzeugen und über Controller verbinden
		final ViewShop viewShop = new ViewShop();

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				viewCustomer = new ViewCustomer();
				viewCustomer.setTimeThreadActive(timeThreadActive);

				ControllerShop controllerShop = new ControllerShop();
				controllerShop.link(modelShop, viewShop);

				ControllerCustomer controllerCustomer = new ControllerCustomer();
				controllerCustomer.link(modelShop, viewCustomer);

				// TimeServer erstellen und Thread starten (Liefert Uhrzeit auf Anfrage)
				TimeServerThread timeST = new TimeServerThread(timeThreadActive);
				Thread serverThread = new Thread(timeST);
				serverThread.start();

			}

		});

	}

}
